﻿using System;
using System.Text;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Data.SqlServerCe;
using KingOfCSharp.iHunter;

namespace KingOfCSharp.iHunter.Database
{
    internal class MessagesRowCustomFields : IDictionary<string, string>
    {
        private MasterDataSet.MessagesRow row;
        public MessagesRowCustomFields(MasterDataSet.MessagesRow row)
        {
            this.row = row;
        }
        #region IDictionary<string,string> 成员

        public void Add(string key, string value)
        {
            if (value == null)
                throw new ArgumentNullException();

            if (this.ContainsKey(key))
                throw new InvalidOperationException();

            this.row.SetCustomField(key, value);
        }

        public bool ContainsKey(string key)
        {
            return (this.row.GetCustomField(key) != null);
        }

        public ICollection<string> Keys
        {
            get
            {
                List<string> keys = new List<string>();
                if (this.row.CustomFields == null)
                    return keys;
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(this.row.CustomFields);
                foreach (XmlNode child in xmlDoc.DocumentElement.ChildNodes)
                {
                    keys.Add(child.Name);
                }
                return keys;
            }
        }

        public bool Remove(string key)
        {
            bool exist = this.ContainsKey(key);
            if (exist)
            {
                this.row.SetCustomField(key, null);
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool TryGetValue(string key, out string value)
        {
            if (!this.ContainsKey(key))
            {
                value = null;
                return false;
            }
            value = this[key];
            return true;
        }

        public ICollection<string> Values
        {
            get
            {
                List<string> values = new List<string>();
                foreach (string key in this.Keys)
                {
                    values.Add(this[key]);
                }
                return values;
            }
        }

        public string this[string key]
        {
            get
            {
                return this.row.GetCustomField(key);
            }
            set
            {
                this.row.SetCustomField(key, value);
            }
        }

        #endregion

        #region ICollection<KeyValuePair<string,string>> 成员

        void ICollection<KeyValuePair<string, string>>.Add(KeyValuePair<string, string> item)
        {
            Add(item.Key, item.Value);
        }

        public void Clear()
        {
            this.row.CustomFields = null;
        }

        bool ICollection<KeyValuePair<string, string>>.Contains(KeyValuePair<string, string> item)
        {
            return ContainsKey(item.Key) && this[item.Key] == item.Value;
        }

        void ICollection<KeyValuePair<string, string>>.CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
        {
            int index = arrayIndex;
            foreach (string key in this.Keys)
            {
                array[index] = new KeyValuePair<string, string>(key, this[key]);
            }
        }

        public int Count
        {
            get { return this.Keys.Count; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        bool ICollection<KeyValuePair<string, string>>.Remove(KeyValuePair<string, string> item)
        {
            if (item.Value != null && this[item.Key] == item.Value)
                return Remove(item.Key);

            return false;
        }

        #endregion

        #region IEnumerable<KeyValuePair<string,string>> 成员

        public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
        {
            foreach (string key in this.Keys)
            {
                yield return new KeyValuePair<string, string>(key, this[key]);
            }
            yield break;
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return (this as IEnumerable<KeyValuePair<string, string>>).GetEnumerator();
        }

        #endregion
    }
}
